// app.js
import i18nInstance from './miniprogram_npm/miniprogram-i18n-plus/index'
import lang from './lang/index'
import {
  getUnmakeList
} from "./services/api/custom"
let domain = require('./services/domain.js')
App({
  onLaunch() {
    this.loadFont()
    try {
      domain.token = JSON.parse(wx.getStorageSync('userInfo')).token

    } catch (error) {

    }


    this.checkVersion()

    i18nInstance.loadTranslations(lang)
    if (wx.getStorageSync('language') == 'en' || wx.getStorageSync('language') == '"en"') {
      this.data.lang = 'en'
      this.data.$language = i18nInstance.locales.en

    } else {
      this.data.lang = 'zh'
      this.data.$language = i18nInstance.locales.zh
    }
    i18nInstance.setLocale(this.data.lang)
    const sysInfo = wx.getSystemInfoSync()
    this.data.systemInfo = sysInfo


    wx.getStorage({
      key: 'userInfo',
      success: (res) => {
        // console.log(res.data)
        if (res.data) {
          const userInfo = JSON.parse(res.data)
          this.data.userInfo = userInfo
          domain.token = userInfo.token
        }
      },
    })



  },
  loadFont(){
    wx.loadFontFace({
      global: true,
      family: 'HarmonyOS_Sans_SC_Light',
      source: 'https://oss.xueleimuseum.com/bin/1084273352364756992_HarmonyOS_Sans_SC_Light.ttf',
      success: res => {
        console.log('字体1加载成功', res)
      }
    })
    wx.loadFontFace({
      global: true,
      family: 'HarmonyOS_Sans_SC_Regular',
      source: 'https://oss.xueleimuseum.com/bin/1084273604413067264_HarmonyOS_Sans_SC_Regular.ttf',
      success: res => {
        console.log('字体1加载成功', res)
      }
    })
  },
  onShow() {},
  languageChange(lang) {

    this.data.$language = i18nInstance.locales[lang]

  },
  checkVersion() {
    let updateManager = wx.getUpdateManager()
    /*updateManager.onCheckFoUpdate(res => {
      // 请求完整版本信息的回调
      // console.log(res.haUpdate) 
    })*/
    updateManager.onUpdateReady(() => {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: (res) => {
          if (res.confirm) updateManager.applyUpdate()
        },
      })
    })
    updateManager.onUpdateFailed(() => wx.showToast({
      title: '软件下载失败...',
      icon: 'none'
    }))
  },
  // 
  getUnmakeList(cb) {
    getUnmakeList().then(res => {
      if (res.code == 0 && res.info.length > 0) {
        this.data.offlineCustom = true
      } else {
        this.data.offlineCustom = false
      }
      cb && cb()
    })
  },
  toTinymceView(item) {
    console.log(item);
    if (item && item.includeSubsets == 1) {

      this.data.TinymceData = {
        advertisingPositionName: item.advertisingPositionName,
        content: item.content,
      }
      wx.navigateTo({
        url: '/subpackages/TinymceView/index',
      })
    }
  },
  data: {
    eventBus: null,
    userInfo: {},
    systemInfo: {},
    cache: {},
    iotData: {},
    ble: null,
    targetWifi: null,
    linkedWifi: null,
    linkedBleDeviceId: '',
    linkedBleAdvertisData: '',
    langs: Object.keys(lang),
    lang: 'zh',
    $language: {},
    likeData: {
      sex: "1",
      age: 24,
      countryId: "218",
      country: "中国",
      countryEn: "China",
      provinceId: "",
      province: '',
      wearPerfume: 1,
      usePerfume: '',
      incenseBase:[]
    },
    customData: {
      perfumeIds: '',
      incenseBaseIds: '',
      showPerfumeIds: ''
    },
    playImgUrlData: {
      imgUrl: '/assets/tmps/pay-top.jpg',
    },
  },
  initPageLang(_this) {
  

    // if (this.data.lang == 'en') {
    //   wx.loadFontFace({
    // global:true,
    //     family: 'family1',
    //     source: 'https://636c-cloud1-2gqiavj3b1f0e068-1312301037.tcb.qcloud.la/fontsEditUse/fontface.ttf',
    //     success: res => {
    //       console.log('字体1加载成功', res)
    //     }
    //   })
    // }else{
    //   // this.setData({ customFontLoaded: false });
    // }
    i18nInstance.effect(_this)
    _this.setData({
      language: i18nInstance.getLocale()
    })
  },
})